﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DialogForm
{
    public partial class FrmWarehouse_dlg : XtraForm
    {
        /// <summary>
        /// 多选
        /// </summary>
        public bool MultiSelect = false;

        public delegate void ReturnListEventHandler(List<DataRow> lists);
        public event ReturnListEventHandler ReturnList;

        public FrmWarehouse_dlg()
        {
            InitializeComponent();
            InitGrid();
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            grvBody.IndicatorWidth = grvBody.IndicatorWidth == -1 ? 40 : grvBody.IndicatorWidth;
            grvBody.OptionsView.ShowIndicator = true;

            CommonControl.DxGridHelper.InitGridStyle(grvBody);
        }

        private void FrmWarehouse_dlg_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            var bll = new DMZS.BLL.Warehouse();
            var ds = bll.GetListForDialog(txtCode.Text.Trim(), txtName.Text.Trim());
            if (ds != null && ds.Tables.Count > 0)
            {
                if (MultiSelect)
                {
                    ds.Tables[0].Columns.Add(new DataColumn("Checked", System.Type.GetType("System.Boolean")));
                }
                else
                {
                    grvBody.Columns["Checked"].Visible = false;
                }

                grdBody.DataSource = ds.Tables[0];
            }
        }

        private void grvBody_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (MultiSelect)
            {
                DataTable dt = grdBody.DataSource as DataTable;

                List<DataRow> list = new List<DataRow>();
                foreach (DataRow row in dt.Rows)
                {
                    if (row["Checked"] != DBNull.Value && Convert.ToBoolean(row["Checked"]))
                    {
                        list.Add(row);
                    }
                }

                if (list.Count == 0) return;
                ReturnList(list);
                this.Close();
            }
            else
            {
                if (grvBody.GetFocusedDataRow() != null)
                {
                    List<DataRow> list = new List<DataRow>();
                    list.Add(grvBody.GetFocusedDataRow());
                    ReturnList(list);
                    this.Close();
                }
            }
        }

        private void grvBody_DoubleClick(object sender, EventArgs e)
        {
            if (MultiSelect)
            {
                object obj = grvBody.GetFocusedRowCellValue("Checked");
                if (obj != DBNull.Value)
                {
                    grvBody.SetFocusedRowCellValue("Checked", !Convert.ToBoolean(obj));
                }
                else
                {
                    grvBody.SetFocusedRowCellValue("Checked", true);
                }
            }
            else
            {
                if (grvBody.GetFocusedRow() != null)
                {
                    List<DataRow> list = new List<DataRow>();
                    list.Add(grvBody.GetDataRow(grvBody.FocusedRowHandle));
                    ReturnList(list);
                    this.Close();
                }
                else
                {
                    return;
                }
            }
        }
    }
}
